package cvi.com.reader.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.adapter.HomePageListAdapter;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.HotBean;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.widgets.MultiStateView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 梅涛 on 2020/7/27.
 * 随机朗读
 */

public class RandomReadingActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tittle)
    TextView tittle;
    @BindView(R.id.mylistview)
    ListView mylistview;
    @BindView(R.id.stateView)
    MultiStateView stateView;
    @BindView(R.id.no_data)
    TextView noData;
    private HomePageListAdapter homePageListAdapter;
    //首页热门数据
    private List<HotBean.RowsBean> homeHotList = new ArrayList<HotBean.RowsBean>();
    private int randomID = -1;
    private int isRandom = -1;
    private String searchStr = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_random_read;
    }

    @Override
    protected void initView() {
        stateView.getBackground().setAlpha(153);
        isRandom = getIntent().getIntExtra("isRandom", -1);
        back.setOnClickListener(this);
        if (1 == isRandom) {
            tittle.setText("随机朗读");

        } else {
            searchStr = getIntent().getStringExtra("searchStr");
            tittle.setText("搜索结果");
        }

    }

    @Override
    protected void initData() {
        if (1 == isRandom) {
            //绑定生命周期
            NetWorkManager.getInstance().getNetWorkApiService().getRandomData().compose(this.<JSONObject>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DefaultObserver<JSONObject>(RandomReadingActivity.this) {
                        @Override
                        protected void onSuccess(JSONObject response) {
                            //获取热门数据
                            setHotData(response);
                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        }
                    });
        } else {
            //绑定生命周期
            NetWorkManager.getInstance().getNetWorkApiService().getSearchData(searchStr).compose(this.<JSONObject>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DefaultObserver<JSONObject>(RandomReadingActivity.this) {
                        @Override
                        protected void onSuccess(JSONObject response) {
                            //获取热门数据
                            setHotData(response);
                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        }
                    });

        }

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void setHotData(JSONObject response) {
        JSONArray array = response.getJSONArray("rows");
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = (JSONObject) array.get(i);     //将array中的数据进行逐条转换
            HotBean.RowsBean rowsBean = (HotBean.RowsBean) JSONObject.toJavaObject(object, HotBean.RowsBean.class);  //通过JSONObject.toBean()方法进行对象间的转换
            homeHotList.add(rowsBean);
        }
        if (homeHotList.size() < 1) {
            noData.setVisibility(View.VISIBLE);
            mylistview.setVisibility(View.GONE);

        } else {
            noData.setVisibility(View.GONE);
            mylistview.setVisibility(View.VISIBLE);
        }
        homePageListAdapter = new HomePageListAdapter(RandomReadingActivity.this, homeHotList);
        mylistview.setAdapter(homePageListAdapter);
        homePageListAdapter.notifyDataSetChanged();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
